import { CategoryResponseData } from '@/api/product/attr/types';
import { CategoryState } from './types/type';

export const useCategoryStore = defineStore('category', {
  state: (): CategoryState => {
    return {
      c1Id: '',
      c2Id: '',
      c3Id: '',
      c1Arr: [],
      c2Arr: [],
      c3Arr: [],
    };
  },
  actions: {
    async getC1() {
      const result: CategoryResponseData = await reqC1();
      if (result.code === 200) {
        this.c1Arr = result.data;
      }
    },
    async getC2() {
      const result: CategoryResponseData = await reqC2(this.c1Id);
      if (result.code === 200) {
        this.c2Arr = result.data;
      }
    },
    async getC3() {
      const result: CategoryResponseData = await reqC3(this.c2Id);
      if (result.code === 200) {
        this.c3Arr = result.data;
      }
    },
    // 一级分类id发生变化的时候清理二三级数据
    clearc2c3() {
      this.c2Id = '';
      this.c2Arr = [];
      this.c3Id = '';
      this.c3Arr = [];
    },
    // 二级分类id发生变化的时候清理三级数据
    clearc3() {
      this.c3Id = '';
      this.c3Arr = [];
    },
  },
});
